package com.elitel.frame.web.common.domain;

import com.elitel.common.core.domain.JsonResult;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.util.List;

/**
 * 返回结果封装
 *
 * @author mulis
 */
@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class JsonTableResult<T> extends JsonResult<T> {

    private static final long serialVersionUID = 1L;
    /**
     * 总记录数
     */
    private Long total;
    /**
     * 结果集
     */
    private List<T> rows;
    /**
     * 忽视非必要字段
     */
    @JsonIgnore
    private T data;


    public static <T> JsonTableResult<T> success(List<T> rows) {
        JsonTableResult<T> jsonResult = new JsonTableResult<>();
        jsonResult.setCode(Codes.SUCCESS.getCode());
        jsonResult.setMsg(Codes.SUCCESS.getMessage());
        jsonResult.setSuccess(true);
        jsonResult.setRows(rows);
        jsonResult.setTotal(new PageInfo<>(rows).getTotal());
        return jsonResult;
    }

}
